/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 */
package com.jeesite.modules.vehicle.service;

import com.alibaba.fastjson.JSONArray;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.general.utils.CosApiUtils;
import com.jeesite.modules.vehicle.dao.CarApiDao;
import com.jeesite.modules.vehicle.entity.CarApi;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试数据Service
 * @author ThinkGem
 * @version 2018-04-22
 */
@Service
public class CarApiService extends CrudService<CarApiDao, CarApi> {

	private DecimalFormat df = new DecimalFormat("0.00");

	/**
	 * 查询列表数据
	 */
	public Page<CarApi> findCarList(CarApi carApi) {
		return super.findPage(carApi);
	}

	/**
	 * 查询车详情
	 * @param carId
	 * */
	public Map getCarDetail(String carId){
		Map map = new HashMap();
		List<CarApi> list = dao.getCarDetail(carId);
		if(list.size()>0){
			JSONArray array1 = new JSONArray();
			JSONArray array2 = new JSONArray();
			JSONArray array3 = new JSONArray();
			JSONArray array4= new JSONArray();
			array1.add(CosApiUtils.URL+list.get(0).getCoverUrl());
			for(int i=0;i<list.size();i++){
				CarApi car = list.get(i);
				if (StringUtils.isNotEmpty(car.getMerchantLogo())) {
					car.setMerchantLogo(CosApiUtils.URL+car.getMerchantLogo());
				}
				car.setCoverUrl(CosApiUtils.URL+car.getCoverUrl());
				// 轮播图
				if("0".equals(car.getImageType())){
					array1.add(CosApiUtils.URL+car.getImageUrl());
				}
				// 详情图
				if("1".equals(car.getImageType())){
					array2.add(CosApiUtils.URL+car.getImageUrl());
				}
			}
			CarApi data = list.get(0);
			for(int i=0;i<10;i++){
				if(i<4){
					Map attr = new HashMap();
					if(i==0){
						attr.put("title","车牌归属");attr.put("value",data.getOriginPlace());
					}
					if(i==1){
						attr.put("title","公里数");attr.put("value",data.getKilometer());
					}
					if(i==2){
						attr.put("title","品牌");attr.put("value",data.getBrandName());
					}
					if(i==3){
						attr.put("title","类型");attr.put("value",data.getTypeName());
					}
					array4.add(attr);
				}
				if(i>3){
					Map maps = new HashMap();
					if(i==4){
						maps.put("title","名称"); maps.put("value",data.getCarTitle());
					}
					if(i==5){
						maps.put("title","颜色"); maps.put("value",data.getCarColor());
					}
					if(i==6){
						maps.put("title","车型"); maps.put("value",data.getCarModel());
					}
					if(i==7){
						maps.put("title","过户次数");maps.put("value",data.getTransfers()+"次");
					}
					if(i==8){
						maps.put("title","看车地点"); maps.put("value",data.getLookPlace());
					}
					if(i==9){
						maps.put("title","汽缸");maps.put("value",data.getCylinder());
					}
					if(i==10){
						maps.put("title","具体车况");maps.put("value",data.getExplain());
					}
					if(i==11){
						maps.put("title","详情联系");maps.put("value",data.getDetailTel());
					}
					array3.add(maps);
				}
            }
			data.setShowSellPrice(df.format(data.getSellPrice()/100));
			map.put("roundList", array1);
			map.put("detailList", array2);
			map.put("dataInfo", list.get(0));
			map.put("colList", array3);
			map.put("rowList", array4);
		}
		return map;
	}

	/**
	 * 插入图片
	 * @param map
	 * */
	@Transactional
	public int insertImage(Map map){
		return dao.insertImage(map);
	}
}